//
//  VoiceReadDetailReplyItemCell.m
//  ShowYork
//
//  Created by hxh on 2021/3/29.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "VoiceReadDetailReplyItemCell.h"
#import "HomePageFeedbackView.h"
@interface VoiceReadDetailReplyItemCell ()
@property (nonatomic,strong)UIImageView *headerIcon;
@property (nonatomic,strong)UILabel *nameLb;
@property (nonatomic,strong)UILabel *contentLb;
@property (nonatomic,strong)UILabel *createDateLb;

@property (nonatomic,strong)UILabel *likeNumLb;
@property (nonatomic,strong)UIImageView *likeImgView;
@property (nonatomic,strong)UIButton *likebtn;

@property (nonatomic,strong)UIView *contentLbView;

@property (nonatomic,strong)UIView *coverView;


@end

@implementation VoiceReadDetailReplyItemCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    
    self.contentLbView = [[UIView alloc]init];
    [self.contentView addSubview:self.contentLbView];

    self.contentLb = [[UILabel alloc]init];
    self.contentLb.textColor = C51;
    self.contentLb.font = font(14);
    self.contentLb.numberOfLines = 0;
    self.contentLb.text = @"-";
    [self.contentView addSubview:self.contentLb];

    self.createDateLb = [[UILabel alloc]init];
    self.createDateLb.textColor = C153;
    self.createDateLb.font = font(10);
    self.createDateLb.text = @"-";
    [self.contentView addSubview:self.createDateLb];


    self.headerIcon = [[UIImageView alloc]init];
    self.headerIcon.layer.cornerRadius = 16;
    self.headerIcon.layer.masksToBounds = YES;
    self.headerIcon.backgroundColor = RGBA(240, 240, 240, 1);
    [self.contentView addSubview:self.headerIcon];


    self.nameLb = [[UILabel alloc]init];
    self.nameLb.textColor  = C102;
    self.nameLb.font = font(12);
    self.nameLb.text = @"-";
    [self.contentView addSubview:self.nameLb];
    

    self.likeNumLb = [[UILabel alloc]init];
    self.likeNumLb.textColor = C153;
    self.likeNumLb.font = font(14);
    self.likeNumLb.text = @"-";
    [self.contentView addSubview:self.likeNumLb];

    
    self.likeImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"campus_like_un_tag"]];
    self.likeImgView.highlightedImage = [UIImage imageNamed:@"campus_like_tag"];
    [self.contentView addSubview:self.likeImgView];
    

    self.likebtn = [[UIButton alloc]init];
    [self.likebtn addTarget:self action:@selector(likeMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.likebtn];
    
    self.coverView = [[UIView alloc]init];
    self.coverView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.coverView];
    
    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLb);
        make.left.equalTo(self.nameLb);
        make.right.equalTo(self.contentView).offset(-16);
        make.bottom.equalTo(self.createDateLb.mas_top);
    }];
    
    [self.headerIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(8);
        make.left.equalTo(self.contentView).offset(16);
        make.width.mas_equalTo(32);
        make.height.mas_equalTo(32);
    }];
    
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerIcon.mas_right).offset(8);
        make.top.equalTo(self.headerIcon);
    }];
    
    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLb);
        make.top.equalTo(self.nameLb.mas_bottom).offset(4);
        make.right.equalTo(self.contentView).offset(-16);
    }];
    
    [self.createDateLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLb.mas_bottom).offset(12);
        make.left.equalTo(self.contentLb);
        make.bottom.equalTo(self.contentView).offset(-8);
    }];
    
    [self.likeNumLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.createDateLb);
        make.right.equalTo(self.contentView).offset(-20);
    }];
    
    [self.likeImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.likeNumLb);
        make.right.equalTo(self.likeNumLb.mas_left).offset(-8);
    }];
    
    [self.likebtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.likeImgView);
        make.right.equalTo(self.contentView);
        make.left.equalTo(self.likeImgView.mas_left);
    }];
    
    [self.contentLbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLb.mas_bottom);
        make.left.right.equalTo(self.contentLb);
        make.bottom.equalTo(self.createDateLb.mas_top).offset(-8);
    }];
    
    UITapGestureRecognizer *headIconTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showUserInfo)];
    self.headerIcon.userInteractionEnabled = YES;
    [self.headerIcon addGestureRecognizer:headIconTap];
    
    UITapGestureRecognizer *userNameTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showUserInfo)];
    self.nameLb.userInteractionEnabled = YES;
    [self.nameLb addGestureRecognizer:userNameTap];
    
    
    self.coverView.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
    [self.coverView addGestureRecognizer:longTap];
    
}

- (void)longPress:(UILongPressGestureRecognizer *)gesture{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        [self coverMethod];
    }
}

- (void)likeMethod{
    if ([RPAppManager isShowNetworkView]) {
        [WHToast showErrorWithMessage:@"网络好像有点问题，请检查后重试" duration:2 finishHandler:nil];
        return;
    }
    if (![RPAppManager isLogin]) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(doLikeWithItemModel:)]) {
            [self.delegate doLikeWithItemModel:self.model];
        }
        return;
    }

    
    BOOL isLike = [self.model.isLike boolValue];
    
    self.model.isLike = @(!isLike);

    self.likeImgView.highlighted = [self.model.isLike boolValue];

    NSInteger max = self.model.commentVM.likeNum.intValue +  ([self.model.isLike boolValue]?1:-1);

    if (max >= 10000) {
        NSInteger count = max;
        if (count % 1000 == 0) {
            count += 1;
        }
        self.likeNumLb.text =  [NSString stringWithFormat:@"%@w",[RPAppManager notRounding:count/10000.0f afterPoint:1]];
    }else{
        self.likeNumLb.text = [NSString stringWithFormat:@"%ld",(long)max];
    }

    self.model.commentVM.likeNum = @(max);

    if (self.delegate && [self.delegate respondsToSelector:@selector(doLikeWithItemModel:)]) {
        [self.delegate doLikeWithItemModel:self.model];
    }
}

- (void)showUserInfo{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showUserInfoWithItemModel:)]) {
        [self.delegate showUserInfoWithItemModel:self.model];
    }
}

+(instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"VoiceReadDetailReplyItemCell";
    VoiceReadDetailReplyItemCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[VoiceReadDetailReplyItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}
-(void)setModel:(VoiceReadCommentListModel *)model{
    _model = model;

    if (model.commentVM.likeNum.intValue >= 10000) {
        NSInteger count = model.commentVM.likeNum.intValue;
        if (count % 1000 == 0) {
            count += 1;
        }
        self.likeNumLb.text = [NSString stringWithFormat:@"%@w",[RPAppManager notRounding:count/10000.0f afterPoint:1]];
    }else{
        self.likeNumLb.text = [NSString stringWithFormat:@"%d",model.commentVM.likeNum.intValue];
    }
    self.contentLb.text = model.commentVM.commentText;
    self.likeImgView.highlighted = [model.isLike boolValue];
    self.nameLb.text = model.userVM.realName;
    self.createDateLb.text = model.commentVM.createDetailTimeStr;

    if (model.display) {
        self.contentLbView.backgroundColor = RGBA(86, 128,250,0.1);
    }else{
        self.contentLbView.backgroundColor = [UIColor whiteColor];
    }
    if (model.userVM.imagePath.length == 0) {
        if (model.userVM.sex.intValue == 0) {
            self.headerIcon.image = [UIImage imageNamed:@"defaut_header"];
        }else if (model.userVM.sex.intValue == 1){
            self.headerIcon.image = [UIImage imageNamed:@"common_boy_tag"];
        }else{
            self.headerIcon.image = [UIImage imageNamed:@"common_girl_tag"];
        }
    }else{
        [self.headerIcon sd_setImageWithURL:[NSURL URLWithString:model.userVM.imagePath]];
    }
}

- (void)coverMethod{
    
    if (![RPAppManager isLogin]) {
        return;
    }
    
    BOOL isMine = [self.model.userVM.id isEqualToNumber:RPAppManager.getCustomerId];
    HomePageFeedbackView *feedbackView = [[HomePageFeedbackView alloc]initWithCompleteBlock:^{
        if (isMine) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(removeReplyWithItemModel:)]) {
                [self.delegate removeReplyWithItemModel:self.model];
            }
        }else{
            if (self.delegate && [self.delegate respondsToSelector:@selector(reportReplyWithItemModel:)]) {
                [self.delegate reportReplyWithItemModel:self.model];
            }
        }
    }];
    feedbackView.title = isMine?@"删除":@"投诉";
    [feedbackView show];
}

@end
